import express from 'express';
import mongoose from 'mongoose';
import cookieParser from 'cookie-parser';
import indexRouter from './routers/index';

const app = express();
// mongoose.set('useFindAndModify', false);

app.use(express.static('public'));

app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(cookieParser());

app.use('/', indexRouter);

mongoose.connect(
    'mongodb://localhost/server_db2',
    {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        useFindAndModify: false
    })
    .then(() => {
        console.log('连接数据库成功!!!')
        // 只有当连接上数据库后才去启动服务器
        app.listen('5000', () => {
            console.log('服务器启动成功, 请访问: http://localhost:5000')
        })
    })
    .catch((error: Error) => {
        console.error('连接数据库失败', error)
    })